package com.aoip.telemetry.telemetry.configureui.business.storage.merge.controller;

import com.aoip.telemetry.telemetry.configureui.business.storage.merge.service.DataMergeService;
import com.aoip.telemetry.telemetry.configureui.common.annotation.Log;
import com.aoip.telemetry.telemetry.configureui.common.core.controller.BaseController;
import com.aoip.telemetry.telemetry.core.entity.AjaxResult;
import com.aoip.telemetry.telemetry.configureui.common.core.page.TableDataInfo;
import com.aoip.telemetry.telemetry.configureui.common.enums.BusinessType;
import com.aoip.telemetry.telemetry.entity.common.MergeTableDataEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author chenbai
 * @version 1.0
 * @date 2022/4/19 20:15
 */
@RestController
public class DataMergeController extends BaseController {

    @Resource
    private DataMergeService service;

    /**
     * 查询合并字段配置列表信息（带分页）
     *
     * @param entity MergeTableDataEntity 实体
     * @return com.aoip.telemetry.telemetry.configureui.common.core.page.TableDataInfo
     * @author chenbai
     * @date 2022/4/19
     **/
    @GetMapping("/v1/GET/data/merge/page")
    public TableDataInfo page(MergeTableDataEntity entity) {
        startPage();
        List<MergeTableDataEntity> list = service.list(entity);
        return getDataTable(list);
    }

    /**
     * 根据id查询单条合并字段配置信息
     *
     * @param id 主键id
     * @return com.aoip.telemetry.telemetry.core.entity.AjaxResult
     * @author chenbai
     * @date 2022/4/19
     **/
    @GetMapping("/v1/GET/data/merge/{id}")
    public AjaxResult detail(@PathVariable String id) {
        return AjaxResult.success(service.detail(id));
    }

    /**
     * 查询合并字段配置列表信息（不带分页）
     *
     * @param entity MergeTableDataEntity 实体
     * @return com.aoip.telemetry.telemetry.core.entity.AjaxResult
     * @author chenbai
     * @date 2022/4/19
     **/
    @GetMapping("/v1/GET/data/merge/list")
    public AjaxResult list(MergeTableDataEntity entity) {
        List<MergeTableDataEntity> list = service.list(entity);
        return AjaxResult.success(list);
    }

    @Log(title = "新增单条合并字段配置信息", businessType = BusinessType.INSERT)
    @PostMapping("/v1/POST/data/merge")
    public AjaxResult add(@Validated @RequestBody MergeTableDataEntity entity) {
        return toAjax(service.insert(entity));
    }

    @Log(title = "修改单条合并字段配置信息", businessType = BusinessType.UPDATE)
    @PutMapping("/v1/PUT/data/merge")
    public AjaxResult edit(@Validated @RequestBody MergeTableDataEntity entity) {
        return toAjax(service.update(entity));
    }

    /**
     * 初始化测站缓存数据到redis中，数据格式（id, elementAdditionField）,（补齐字段数据Id，补齐字段实体）
     */
    @Log(title = "初始化配置数据到redis", businessType = BusinessType.INSERT)
    @PostMapping("/v1/POST/data/merge/cache")
    public AjaxResult initCache() {
        service.initCache();
        return AjaxResult.success();
    }

    /**
     * 删除单条或多条合并字段配置信息
     *
     * @param ids 单个或多个主键id
     * @return com.aoip.telemetry.telemetry.core.entity.AjaxResult
     * @author chenbai
     * @date 2022/4/19
     **/
    @Log(title = "删除单条或多条合并字段配置信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/v1/DELETE/data/merge/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        service.delete(ids);
        return success();
    }

}
